#ifndef Q2_H
#define Q2_H
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <ostream>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>
namespace q2 {
    struct Patient
    {
	    std::string name;
	    size_t age;
	    size_t smokes;
	    size_t area_q;
	    size_t alkhol;
        friend void operator>>( std::string s , Patient &p );
        friend std::ostream & operator<<(std::ostream & os ,const Patient &p);
        friend std::ostream & operator<<(std::ostream & os , Patient &&p);

    };
    inline std::ostream & operator<<(std::ostream & os ,const Patient &p) {
        os  << p.name << "|"
            << p.age  << " "
            << p.smokes << " "
            << p.area_q << " "
            << p.alkhol;
        return os;
    }
    inline std::ostream & operator<<(std::ostream & os , Patient &&p) {
        os  << p.name << "|"
            << p.age  << " "
            << p.smokes << " "
            << p.area_q << " "
            << p.alkhol << std::endl;
        return os;
    }
    inline void operator>>( std::string s , Patient &p ) {

        std::replace_if( s.begin() , s.end() , []( char a ) {
            return  a == ',';
        },' ');

        std::istringstream iss(s);

        std::string fname,lname;
        iss >> fname;
        iss >> lname;
        p.name = fname + " " + lname;

        iss >> p.age;
        iss >> p.smokes;
        iss >> p.area_q;
        iss >> p.alkhol;
    }
    static std::vector<Patient> read_file(std::string filename) {
        std::fstream fs(filename);

        std::string header;
        std::getline(fs , header);

        std::string items;
        std::getline(fs , items);

        std::string entry;
        std::vector<Patient> vp;
        do {
            std::getline(fs,entry);
            Patient p;
            entry >> p;
            vp.push_back(p);
        }while (!fs.eof());

        return vp;
    };
    
    inline std::vector<Patient> & sort(std::vector<Patient> &vp ) {
        std::sort(vp.begin(),vp.end(),[](const Patient pa, const Patient pb ) {
            size_t key_a = 3*pa.age + 5*pa.smokes + 2*pa.area_q + 4*pa.alkhol;
            size_t key_b = 3*pb.age + 5*pb.smokes + 2*pb.area_q + 4*pb.alkhol;
            return key_a > key_b;
        });
        return vp;
    }
}

#endif //Q2_H